package com.cakeclient;


import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.cakeclient.Model.Basket;
import com.cakeclient.Model.ProductType;

import java.util.List;

public class ListViewClientBasketAdapter extends ArrayAdapter<ProductType> {
    int resource;
    Context context;
    boolean changeColor = true;
    int myGrey = Color.rgb(231, 227, 231);
    int myBlue = Color.rgb(198, 219, 222);

    public ListViewClientBasketAdapter(Context context, int textViewResourceId, List<ProductType> productTypes) {
        super(context, textViewResourceId, productTypes);
        this.resource = textViewResourceId;
        this.context = context;
    }

    // On override ArrayAdapter
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Récuperer l'order courant
        final Object object = getItem(position);
        LinearLayout objectView;

        // Récuperer le produit courant
        final ProductType productType = getItem(position);


        // Inflate the catalog_detail_product
        if (convertView == null) {
            objectView = new LinearLayout(getContext());
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(inflater);
            vi.inflate(resource, objectView, true);
        } else {
            objectView = (LinearLayout) convertView;
        }

        // Récupérer les View

        // Pour les produits: une image du produit et le nom
        ImageView productImageView = (ImageView) objectView.findViewById(R.id.listview_basket_imageview);
        TextView productTextView = (TextView) objectView.findViewById(R.id.listview_basket_productname);
        TextView productPrice = (TextView) objectView.findViewById(R.id.listview_basket_productprice);
        LinearLayout productLayout = (LinearLayout) objectView.findViewById(R.id.listview_basket_product_layout);


        productTextView.setText(productType.getName());
        if (changeColor) {
            productLayout.setBackgroundColor(myGrey);
            changeColor = !changeColor;
        } else {
            productLayout.setBackgroundColor(myBlue);
            changeColor = !changeColor;
        }


        // Ajout du listener pour ajouter un produit au panier
        Button buttonRemove = (Button) objectView.findViewById(R.id.button_del);
        buttonRemove.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Basket basket = ((MainActivity) context).basket;
                basket.getProducts().remove(object);
                Toast toast = Toast.makeText(getContext(), "Product Deleted", Toast.LENGTH_SHORT);
                toast.show();
                ((MainActivity) context).LaunchBasketFragment();
            }
        });


        return (objectView);


    }
}
